﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;
using BidFunLib.DAO;


namespace BidFunLib
{
    /// <summary>
    /// 
    /// </summary>
    class StatsBidObjectGlobalDAO 
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(StatsBidObjectGlobalDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StatsBidObjectGlobal getStatsBidObjectGlobalByIdObject(Int32 idObject)
        {
            StatsBidObjectGlobal statsBidObjectGlobal = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<StatsBidObjectGlobal> statsBidObjectGlobalList = (IList<StatsBidObjectGlobal>)session.CreateQuery("from StatsBidObjectGlobal sbog where sbog.IdBidObject= ?")
                .SetInt32(0, idObject)
                .List<StatsBidObjectGlobal>();

                if (statsBidObjectGlobalList != null && statsBidObjectGlobalList.Count > 0)
                {
                    statsBidObjectGlobal = statsBidObjectGlobalList.First();
                }
                transaction.Commit();
                session.Close();
                return statsBidObjectGlobal;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        public StatsBidObjectGlobal saveOrUpdate(StatsBidObjectGlobal statsBidObjectGlobal)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                if (statsBidObjectGlobal.IdStatsBidObjectGlobal == -1)
                {
                    log.Debug("Saving statsBidObjectGlobal...");
                    Int32 newID = (Int32)session.Save(statsBidObjectGlobal);
                    statsBidObjectGlobal.IdStatsBidObjectGlobal = newID;
                    log.Debug("statsBidObjectGlobal saved !");
                }
                else
                {
                    log.Debug("Updating statsBidObjectGlobal...");
                    session.Update(statsBidObjectGlobal);
                    log.Debug("statsBidObjectGlobal updated !");
                }
                transaction.Commit();
                session.Close();
                return statsBidObjectGlobal;
            }
        }


    }
}
